<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单日期">
        <el-date-picker
          v-model="daterangeOrderDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="收货人" prop="receiver">
        <el-input
          v-model="queryParams.receiver"
          placeholder="请输入收货人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          maxlength="11"
          show-word-limit
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="请选择订单状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in orderStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supplier:order:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supplier:order:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['supplier:order:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supplier:order:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column
        label="订单编号"
        align="center"
        prop="orderNumber"
        min-width="200px"
      />
      <el-table-column
        label="订单日期"
        align="center"
        prop="orderDate"
        min-width="90px"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="收货人" align="center" prop="receiver" />
      <el-table-column
        label="手机号码"
        align="center"
        prop="phone"
        min-width="100px"
      />
      <el-table-column
        label="订单状态"
        align="center"
        prop="orderStatus"
        :formatter="orderStatusFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-show="scope.row.orderStatus === 0"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supplier:order:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-show="scope.row.orderStatus === 0"
            v-hasPermi="['supplier:order:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['supplier:order:view']"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      :fullscreen="true"
      append-to-body
      :before-close="handleClose"
    >
      <el-steps :active="active" finish-status="success">
        <el-step title="选择商品"></el-step>
        <el-step title="填写收货人"></el-step>
        <el-step title="确认信息"></el-step>
      </el-steps>
      <!--商品信息-->
      <el-card class="box-card" v-show="isShowOne">
        <div slot="header" class="clearfix">
          <span>商品信息</span>
          <el-button
            style="float: right"
            type="primary"
            @click="nextStepOne"
            size="mini"
            :disabled="this.tbOrderDetailList.length > 0 ? false : true"
            >下一步</el-button
          >
        </div>
        <div>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleProduct"
                v-show="!showView"
                >添加</el-button
              >
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="handleDeleteTbOrderDetail"
                :disabled="multipleProduct"
                v-show="!showView"
                >删除</el-button
              >
            </el-col>
          </el-row>
          <el-table
            :data="tbOrderDetailList"
            :row-class-name="rowTbOrderDetailIndex"
            @selection-change="handleTbOrderDetailSelectionChange"
            border
            show-summary
            ref="tbOrderDetail"
          >
            <el-table-column type="selection" width="60" align="center" />
            <el-table-column
              label="序号"
              align="center"
              prop="index"
              width="50"
            />
            <el-table-column label="商品分类" prop="typeName" />
            <el-table-column
              label="商品名称"
              prop="productName"
              min-width="90px"
            />
            
            <el-table-column label="产地" prop="origin" />
            <el-table-column label="生产日期" prop="productDate" />
            <el-table-column label="保质期限" prop="shelfLife">
              <template slot-scope="scope">
                {{ scope.row.shelfLife }} {{ lifeTypeFormat(scope.row) }}
              </template>
            </el-table-column>
            <el-table-column label="单价" prop="marketPrice" min-width="60px">
              <template slot-scope="scope">
                {{ scope.row.marketPrice }}
              </template>
            </el-table-column>
            <el-table-column label="数量" prop="qty" min-width="100px">
              <template slot-scope="scope">
                <el-input-number
                  v-model="scope.row.qty"
                  controls-position="right"
                  @change="handleQtyChange(scope.row)"
                  placeholder="请输入数量"
                  size="mini"
                  :step="1"
                  step-strictly
                  :min="1"
                  :disabled="showView"
                />
              </template>
            </el-table-column>
            <el-table-column label="金额" prop="amount" />
          </el-table>
        </div>
      </el-card>
      <!--收货人信息-->
      <el-card class="box-card" v-show="isShowTwo">
        <div slot="header" class="clearfix">
          <span>填写收货人</span>
          <el-button
            style="float: right"
            type="success"
            @click="submitForm"
            size="mini"
            >下一步</el-button
          >
          <el-button
            style="float: right"
            type="primary"
            @click="prevStepOne"
            size="mini"
            >上一步</el-button
          >
        </div>

        <div>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="订单日期" prop="orderDate">
              <el-date-picker
                clearable
                v-model="form.orderDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择订单日期"
                :disabled="showView"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="总金额" prop="totalAmount">
              <el-input-number
                v-model="form.totalAmount"
                controls-position="right"
                placeholder="请输入总金额"
                :precision="2"
                :disabled="true"
              />（元）
            </el-form-item>

            <el-form-item label="收货人" prop="receiver">
              <el-input
                v-model="form.receiver"
                placeholder="请输入收货人"
                maxlength="10"
                show-word-limit
                clearable
                :disabled="showView"
              >
                <el-button
                  slot="append"
                  icon="el-icon-thumb"
                  @click="queryAddress()"
                  :disabled="showView"
                  >选择</el-button
                >
              </el-input>
            </el-form-item>

            <el-form-item label="手机号码" prop="phone">
              <el-input
                v-model="form.phone"
                placeholder="请输入手机号码"
                maxlength="11"
                show-word-limit
                clearable
                :disabled="showView"
              />
            </el-form-item>

            <el-form-item label="收货地址" prop="receiverAddress">
              <el-input
                v-model="form.receiverAddress"
                placeholder="请输入收货地址"
                maxlength="120"
                show-word-limit
                clearable
                :disabled="showView"
              />
            </el-form-item>

            <el-form-item label="订单状态" prop="orderStatus">
              <el-select
                v-model="form.orderStatus"
                placeholder="请选择订单状态"
                :disabled="true"
              >
                <el-option
                  v-for="dict in orderStatusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                maxlength="250"
                show-word-limit
                rows="5"
                :disabled="showView"
              />
            </el-form-item>
          </el-form>
        </div>
      </el-card>

      <el-card class="box-card" v-show="isShowThree">
        <div slot="header" class="clearfix">
          <span>确认信息</span>
          <el-button
            style="float: right"
            type="success"
            @click="nextStepFinish"
            size="mini"
            >完成</el-button
          >
          <el-button
            style="float: right"
            type="primary"
            @click="prevStepTwo"
            size="mini"
            v-show="showView"
            >上一步</el-button
          >
        </div>
        <div>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="订单编号" prop="orderNumber">
              <el-input
                v-model="form.orderNumber"
                placeholder="请输入订单编号"
                :readonly="true"
                clearable
                :disabled="true"
              />
            </el-form-item>

            <el-form-item label="订单日期" prop="orderDate">
              <el-date-picker
                clearable
                :readonly="true"
                v-model="form.orderDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择订单日期"
                :disabled="showView"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="总金额" prop="totalAmount">
              <el-input-number
                v-model="form.totalAmount"
                placeholder="请输入总金额"
                controls-position="right"
                :precision="2"
                :disabled="true"
              />（元）
            </el-form-item>
          </el-form>
        </div>
      </el-card>

      <!--商品信息-->
      <el-dialog
        :visible.sync="addProductVisiable"
        title="商品信息"
        width="800px"
        append-to-body
      >
       <el-form :model="listQueryProduct" ref="listQueryProduct" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="商品分类" prop="typeId">
          <el-select v-model="listQueryProduct.typeId" placeholder="请选择商品分类" clearable size="small">
              <el-option
                v-for="item in productTypeList"
                :key="item.id"
                :label="item.typeName"
                :value="item.id"
              />
            </el-select>
        </el-form-item>
         <el-form-item label="商品名称" prop="productName">
            <el-input
              v-model="listQueryProduct.productName"
              clearable
              class="filter-item"
              style="width: 200px"
              placeholder="请输入商品名称"
            />
         </el-form-item>
       </el-form>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilterProduct"
          size="mini"
          >查找</el-button
        >
        <el-table
          v-loading="loading"
          :data="productList"
          element-loading-text="正在查询中。。。"
          border
          fit
          highlight-current-row
          ref="productListDetail"
          @selection-change="handleProductSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="商品分类" align="center" prop="typeName" />
          <el-table-column label="商品编号" align="center" prop="productSn" />
          <el-table-column label="商品名称" align="center" prop="productName" />
          <el-table-column label="产地" align="center" prop="origin" />
          <el-table-column label="品牌名称" align="center" prop="brandDesc" />
          <el-table-column label="单价" align="center" prop="marketPrice" />
          <el-table-column
            label="生产日期"
            align="center"
            prop="productDate"
            min-width="90px"
          />
          <el-table-column label="保质期限" align="center" prop="shelfLife">
            <template slot-scope="scope">
              {{ scope.row.shelfLife }} {{ lifeTypeFormat(scope.row) }}
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="totalProduct > 0"
          :total="totalProduct"
          :page.sync="listQueryProduct.pageNum"
          :limit.sync="listQueryProduct.pageSize"
          @pagination="getProductList"
        />
        <div slot="footer" class="dialog-footer">
          <el-button @click="addProductVisiable = false">取消</el-button>
          <el-button type="primary" @click="handleSpecificationAdd"
            >确定</el-button
          >
        </div>
      </el-dialog>

      <!--弹出对话框选择收货地址-->
      <el-dialog :visible.sync="addVisiable" title="收货地址" append-to-body>
        <div class="search">
          <el-input
            v-model="listQuery.addArea"
            clearable
            class="filter-item"
            style="width: 200px"
            placeholder="请输入详细地址"
          />
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
            size="mini"
            >查找</el-button
          >
          <el-table
            v-loading="loading"
            :data="addressList"
            element-loading-text="正在查询中。。。"
            border
            fit
            highlight-current-row
          >
            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="confirmAdd(scope.row)"
                  >选择</el-button
                >
              </template>
            </el-table-column>
            <el-table-column align="center" label="ID" prop="id" />
            <el-table-column
              label="行政省/市/县区"
              align="center"
              min-width="150px"
            >
              <template slot-scope="scope">
                {{
                  scope.row.provinceName +
                  scope.row.cityName +
                  scope.row.countyName
                }}
              </template>
            </el-table-column>
            <el-table-column
              label="详细地址"
              align="center"
              prop="addArea"
              min-width="300px"
            />
            <el-table-column label="收货人" align="center" prop="addReceiver" />
            <el-table-column
              label="手机号"
              align="center"
              prop="phone"
              min-width="120px"
            />
            <el-table-column label="是否默认" align="center">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.defaultAddress"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-value="1"
                  inactive-value="0"
                  @change="handleStatusChange(scope.row)"
                ></el-switch>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="totalAddress > 0"
            :total="totalAddress"
            :page.sync="listQuery.pageNum"
            :limit.sync="listQuery.pageSize"
            @pagination="getAddressList"
          />
        </div>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  exportOrder,
  generateNumber,
} from "@/api/supplier/order";
import { listAddress, getAddressDefault } from "@/api/supplier/address";
import { listPrice } from "@/api/supplier/price";
import { listProductType} from "@/api/supplier/productType";

export default {
  name: "Order",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,

      // 选中数组
      ids: [],
      // 选中的商品明细
      idsDetail: [],
      // 子表选中数据
      checkedTbOrderDetail: [],
      // 商品分类表格数据
      productTypeList: [],
      // 计量单位字典
      measurementOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 非多个禁用商品信息
      multipleProduct: true,
      // 查看标识
      showView: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品总条数
      totalProduct: 0,
      // 收货地址总条数
      totalAddress: 0,

      // 订单表格数据
      orderList: [],
      // 订单明细表格数据
      tbOrderDetailList: [],
      checkedProduct: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 订单日期时间范围
      daterangeOrderDate: [],
      // 订单状态字典
      orderStatusOptions: [],
      // 保质期限年月日字典
      lifeTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        orderDate: null,
        totalAmount: null,
        receiver: null,
        receiverAddress: null,
        phone: null,
        orderStatus: null,
        orderByColumn: "id",
        isAsc: "desc"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderDate: [
          {
            required: true,
            message: "订单日期不能为空",
          },
        ],
        receiver: [
          {
            required: true,
            message: "收货人不能为空",
          },
        ],
        phone: [
          {
            required: true,
            message: "手机号码不能为空",
          },
        ],
        receiverAddress: [
          {
            required: true,
            message: "收货地址不能为空",
          },
        ],
      },
      //默认隐藏地址对话框
      addVisiable: false,
      addProductVisiable: false,

      active: 0,
      isShowOne: true,
      isShowTwo: false,
      isShowThree: false,
      listQueryProduct: {
        pageNum: 1,
        pageSize: 10,
        ids: [],
      },
      // 收货地址数据
      addressList: [],

      productList: [],
      listQuery: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("bus_order_status").then((response) => {
      this.orderStatusOptions = response.data;
    });
    this.getDicts("bus_measurement").then((response) => {
      this.measurementOptions = response.data;
    });
    this.getDicts("bus_life_type").then((response) => {
      this.lifeTypeOptions = response.data;
    });
    listProductType().then(response => {
        this.productTypeList = response.rows;
    });
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOrderDate && "" != this.daterangeOrderDate) {
        this.queryParams.params["beginOrderDate"] = this.daterangeOrderDate[0];
        this.queryParams.params["endOrderDate"] = this.daterangeOrderDate[1];
      }
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取默认地址
    getAddressDefaultForm() {
      getAddressDefault().then((response) => {
        if (response.data != null) {
          this.form.receiver = response.data.addReceiver;
          this.form.phone = response.data.phone;
          this.form.receiverAddress =
            response.data.provinceName +
            response.data.cityName +
            response.data.countyName +
            response.data.addArea;
        }
      });
    },

    // 保质日期 年月日
    lifeTypeFormat(row, column) {
      return this.selectDictLabel(this.lifeTypeOptions, row.shelfLifeType);
    },
    // 订单状态字典翻译
    orderStatusFormat(row, column) {
      return this.selectDictLabel(this.orderStatusOptions, row.orderStatus);
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        orderDate: null,
        totalAmount: null,
        receiver: null,
        receiverAddress: null,
        phone: null,
        orderStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.listQueryProduct.ids = [];
      this.idsDetail = [];
      this.tbOrderDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.ids = selection.map((item) => {
        if(item.orderStatus>0){
           this.multiple=true;
        }
        return item.id
      });
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // 订单状态默认 创建
      this.form.orderStatus = 0;
      this.open = true;
      this.showView = false;
      this.title = "添加订单";
      this.isShowOne = true;
      this.isShowTwo = false;
      this.isShowThree = false;
      this.form.orderDate=new Date();
      this.active = 0;
      this.getAddressDefaultForm();
     
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then((response) => {
        this.form = response.data;
        if(this.form.orderStatus>0){
          this.showView = true;
        }else{
           this.showView = false;
        }
        
        this.tbOrderDetailList = response.data.tbOrderDetailList;
        this.open = true;
        this.title = "修改订单";
        this.isShowOne = true;
        this.isShowTwo = false;
        this.isShowThree = false;
        this.active = 0;
        this.getProductList();
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then((response) => {
        this.form = response.data;
        this.tbOrderDetailList = response.data.tbOrderDetailList;
        this.open = true;
        this.title = "查看订单";
        this.isShowOne = true;
        this.isShowTwo = false;
        this.isShowThree = false;
        this.active = 0;
        this.showView = true;
        this.getProductList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (!this.showView) {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            this.form.tbOrderDetailList = this.tbOrderDetailList;

            if (this.form.id != null) {
              updateOrder(this.form).then((response) => {
                this.form = response.data;
              });
            } else {
              addOrder(this.form).then((response) => {
                this.form = response.data;
              });
            }
          }
        });
      }

      this.isShowTwo = false;
      this.isShowThree = true;
      this.active = 2;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除订单编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 订单明细序号 */
    rowTbOrderDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 订单明细添加按钮操作 */
    handleAddTbOrderDetail() {
      let obj = {};
      obj.productName = "";
      obj.productId = "";
      obj.specKey = "";
      obj.measurement = "";
      obj.qty = "";
      obj.productDate = "";
      obj.shelfLife = "";
      obj.amount = "";
      this.tbOrderDetailList.push(obj);
    },
    /** 订单明细删除按钮操作 */
    handleDeleteTbOrderDetail(row) {
      if (this.checkedTbOrderDetail.length == 0) {
        this.$alert("请先选择要删除的订单明细数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.checkedTbOrderDetail.forEach((item) => {
          for (let i = 0; i < this.tbOrderDetailList.length; i++) {
            if (this.tbOrderDetailList[i].id === item.id) {
              this.tbOrderDetailList.splice(i, 1);
              this.idsDetail.splice(i, 1);
              break;
            }
          }
        });

        this.checkedTbOrderDetail = [];
      }
    },
    /** 多选框选中数据 */
    handleTbOrderDetailSelectionChange(selection) {
      this.checkedTbOrderDetail = selection;
      this.multipleProduct = !selection.length;
    },
    /** 多选框选中数据 */
    handleProductSelectionChange(selection) {
      this.checkedProduct = selection;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有订单数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportOrder(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    nextStepOne() {
      // 订单明细需要有数据
      if (this.tbOrderDetailList.length > 0) {
        let amount = 0;
        let flag = true;

        this.tbOrderDetailList.some((item) => {
          if (item.qty != null && item.qty != 0) {
            amount = this.$math.add(
              amount,
              this.$math.multiply(item.marketPrice, item.qty).toFixed(2)
            );
          } else {
            this.msgError("【" + item.productName + "】的数量不为空");
            flag = false;
            return true;
          }
        });
        if (flag) {
          this.form.totalAmount = amount;
          this.isShowOne = false;
          this.isShowTwo = true;
          this.active = 1;
        }
      } else {
        this.msgWarning("请选择需要订购的商品");
      }
    },

    prevStepTwo() {
      this.isShowOne = false;
      this.isShowTwo = true;
      this.isShowThree = false;
      this.active = 1;
    },

    handleClose() {
      this.queryParams.pageNum = 1;
      this.open = false;
      this.reset();
      this.getList();
    },

    prevStepOne() {
      this.isShowOne = true;
      this.isShowTwo = false;
      this.active = 0;
    },

    nextStepFinish() {
      if (!this.showView) {
        this.form.orderStatus = 1000;
        updateOrder(this.form).then((response) => {
          this.msgSuccess("订单提交成功");
          this.getList();
        });
      }

      this.open = false;
    },

    handleProduct() {
      this.addProductVisiable = true;
      this.getProductList();
    },
    handleQtyChange(row) {
      // row.amount= row.marketPrice * row.qty
      row.amount = this.$math.multiply(row.marketPrice, row.qty).toFixed(2);
    },
    handleFilter() {
      this.listQuery.pageNum = 1;
      this.getAddressList();
    },
    handleFilterProduct() {
      this.listQueryProduct.pageNum = 1;
      this.getProductList();
    },
    /** 查询收货地址列表 */
    getAddressList() {
      this.loading = true;
      listAddress(this.listQuery).then((response) => {
        this.addressList = response.rows;
        this.totalAddress = response.total;
        this.loading = false;
      });
    },

    /** 查询商品列表 */
    getProductList() {
      this.loading = true;

      if (this.idsDetail.length > 0) {
        for (let i = 0; i < this.idsDetail.length; i++) {
          this.listQueryProduct.ids.push(this.idsDetail[i]);
        }
      }
      if (this.tbOrderDetailList.length > 0) {
        this.tbOrderDetailList.map((item) => {
          this.listQueryProduct.ids.push(item.priceId);
        });
      }

      listPrice(this.listQueryProduct).then((response) => {
        this.productList = response.rows;
        this.totalProduct = response.total;
        this.loading = false;
      });
    },

    //在所有商品中选中对应商品进入订单列表的商品中
    handleSpecificationAdd() {
      this.checkedProduct.forEach((item) => {
        for (let i = 0; i < this.productList.length; i++) {
          if (this.productList[i].priceId === item.priceId) {
            this.productList.splice(i, 1);
            this.tbOrderDetailList.push(item);
            this.idsDetail.push(item.priceId);
            break;
          }
        }
      });

      this.checkedProduct = [];

      this.addProductVisiable = false;
    },
    queryAddress() {
      this.listQuery = {
        pageNum: 1,
        pageSize: 10,
        id: undefined,
        brandDesc: undefined,
        sort: "brand_sort",
        order: "asc",
      };

      this.getAddressList();
      this.addVisiable = true;
    },
    /** 选择对应收货地址 */
    confirmAdd(row) {
      this.form.receiver = row.addReceiver;
      this.form.phone = row.phone;
      this.form.receiverAddress =
        row.provinceName + row.cityName + row.countyName + row.addArea;
      this.addVisiable = false;
    },
  },
};
</script>
